<?php
namespace app\admin\controller;

use app\admin\model\Car as CarModel;
use app\admin\model\Account as AccountModel;
use app\admin\model\Device as DeviceModel;
use think\Db;
use think\Request;

class CarData extends Base
{
    public function index()
    {
        $model = new CarModel;
        $aModel = new AccountModel;
        $dModel = new DeviceModel;

        //渲染页面模板
        $result = $model->field('c.id,c.car_brand,c.car_model,c.plate_number, c.address,c.created_at,c.updated_at, u.username, d.name as dname, d.sn')
            ->alias('c')
            ->join('user u','c.user_id = u.id ')
            ->join('device d','c.device_id = d.id')
            ->order('c.id','desc')
        //->select();
            ->paginate(10);

        //渲染用户组
        $account = $aModel->field('id,username')->select();
        //渲染代理商
        $device = $dModel->field('id,name,sn')->select();

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $_POST["viewId"];
            $view = $model->getAll()->where("id",$id)->select();
            return json([
                'code' => 200,
                'view'=>$view,
            ]);
        }


        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('account',$account);
        $this->assign('device',$device);
        $this->assign('cardata',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }
}